package gui;

import java.awt.BorderLayout;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import application.Puzzle;


public class MainPanel extends JPanel {
	
	private List<Puzzle> puzzles = Puzzle.readPuzzles();
	private int currentIdx = 0;
	private Puzzle current = puzzles.get(currentIdx);
	
	private MenuPanel menuPanel = new MenuPanel(this);
	private PuzzlePanel puzzlePanel = new PuzzlePanel(this, current);
	
	
	public MainPanel() {
		initPanel();
	}
	
	private void initPanel() {
		setLayout(new BorderLayout());
		
		add(menuPanel, BorderLayout.PAGE_START);
		add(puzzlePanel, BorderLayout.CENTER);
	}
	
	
	private void switchCurrent(int idx) {
		currentIdx = idx;
		current = puzzles.get(currentIdx);
		
		menuPanel.resetTime();
		
		showCurrent();
	}
	
	private void showCurrent() {
		puzzlePanel.setPuzzle(current);
		validate();
	}
	
	
	public int numberOfPuzzles() {
		return puzzles.size();
	}
	
	
	public void setCurrent(int idx) {
		switchCurrent(idx - 1);
	}
	
	public void setGuessed(String keyword, boolean isGuessed) {
		current.setGuessed(keyword, isGuessed);
		
		showCurrent();
		
		/*if(current.isWon())
			askNextPuzzle();*/
	}
	
	
	private void askNextPuzzle() {
		Integer[] choices = new Integer[puzzles.size()];
		for(int i = 0; i < choices.length; i++)
			choices[i] = i + 1;
		
		Integer next = Math.min(currentIdx + 2, puzzles.size());
		
		Object choice = JOptionPane.showInputDialog(
				null, 
				"Open next puzzle?",
				"Puzzle completed",
				JOptionPane.QUESTION_MESSAGE,
				null,
				choices,
				next);
		
		if(choice != null)
			setCurrent((Integer) choice);
	}

}
